package com.bw.zhanggm.file.controller;


import cn.hutool.http.HttpUtil;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.bw.zhanggm.file.config.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-01-12
 */
@RestController
@RequestMapping("/file")
public class FilesController {

    @Autowired
    private FileService fileService;


    @PostMapping("upload")
    public AjaxResult upload(@ModelAttribute Chunk chunk) throws InterruptedException {
        int i = fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            TimeUnit.SECONDS.sleep(2);
            FileResult fileResult = fileService.mergeFile(chunk);
            return AjaxResult.success(fileResult);
        }

//        String s = HttpUtil.get();
        return i == 1 ? AjaxResult.success() : AjaxResult.error();
    }

}
